<?php

namespace Model;

use Lib;

class JobModel
{

        private $db = null;
        private $table = 'tbjob';
        public function __construct()
        {
            $this->db = Lib\Db\DbModel::init(); // 构造方法，线上自动切换
        }

        /**
         * 根据种族Id以及版本Id获取种族信息
         * @param $raceId
         * @param $versionId
         * @return array
         */
        public function getJobInfo($raceId,$where)
        {

            $where['jobId'] = $raceId;
            $race = $this->db->table($this->table)->where($where)->find();
            return $race;
        }

        /**
         * 根据版本Id获取对应的版本种族
         * @param $versionId
         * @return array
         */
        public function getAllJob($where)
        {

            $race = $this->db->table($this->table)->where($where)->order("jobId","asc")->select();
            return $race;
        }

    //检查TFTID是否已经存在了
    public function checkTFTID($jobId,$where,$TFTID)
    {
        $where['TFTID'] = $TFTID;
        $where['jobId'] = ['!=',$jobId];

        $res = $this->db->table($this->table)->where($where)->find();
        return $res;
    }
        /**
         * 修改种族信息
         * @param $versionId
         * @param $raceId
         * @param $data
         */
        public function updateJob($jobId,$where,$data)
        {

            $where['jobId'] = $jobId;
            return $this->db->table($this->table)->where($where)->update($data);
        }


        public function insertJob($versionId,$seasonId,$data)
        {
            $arr_keys = array_keys($data[0]);
            array_push($arr_keys,'versionId');
            array_push($arr_keys,'seasonId');
            $sql = 'INSERT INTO '.$this->table .' (' . implode(',' ,$arr_keys) . ') values';
            foreach ($data as &$time){
                $time['versionId'] = $versionId;
                $time['seasonId'] = $seasonId;
                $time['level'] = json_encode($time['level']);

                $arr_values = array_values($time);
                $sql.= " ('" . implode("','" ,$arr_values) . "'),";
            }
            $sql = substr($sql ,0 ,-1);


            $result = $this->db->table($this->table)->exec($sql);

            //echo $this->db->getLastSql();

            var_dump($result); exit();
        }

    public function insertJobOne($versionId,$seasonId,$data)
    {
        $arr_keys = array_keys($data);
        array_push($arr_keys,'versionId');
        array_push($arr_keys,'seasonId');
        $sql = 'INSERT INTO '.$this->table .' (' . implode(',' ,$arr_keys) . ') value';
        $data['versionId'] = $versionId;
        $data['seasonId'] = $seasonId;
        $data['level'] = json_encode($data['level']);

        $arr_values = array_values($data);
        $sql.= " ('" . implode("','" ,$arr_values) . "'),";
        $sql = substr($sql ,0 ,-1);
        $result = $this->db->table($this->table)->exec($sql);
        return $result;
    }

    public function getFormatJob($where)
    {
        $data = $this->db->table($this->table)->where($where)->order("jobId","asc")->select('jobId,name');
        $res = [];
        foreach ($data as $item) {
            $res[$item['jobId']] = $item['name'];
        }
        return $res;
    }
}